#include <stdio.h>
#include <stdlib.h>

int main(int argc, char*argv[]) {
  /* for the 2lpt parameter file, the parameters that need updating for each different run are: i)   seed, ii)  Fnl, iii) qsig
  these will also determine the (full)name of the IC file
  Let's put Np1 and Lbox as options too!
  The output filename:
  2lpt_seed#_fnl_qsig^-1.param
  The set of arguments necessary are:
    * seed
    * Fnl
    * qsig
    * Np1
    * Lbox
    * OutputDir
    * nproc
  */
  if (argc!=6) {
    printf("I need 5 arguments instead of %d ! (seed, Np1, Lbox, OutputDir, nproc)\n", argc-1);
    return;
  }
  else {
    // define the five input variables and other necessary variables.
    int seed, Np1, nproc, Lbox;
    char fname[100];
    char filebase[100];
    FILE* fp;
    
    int redshift=49;
        
    seed = atoi(argv[1]);
    Np1 = atoi(argv[2]);
    Lbox = atoi(argv[3]);
    nproc = atoi(argv[5]);
    // Now write these to the output file, one has to enter the appropriate direcotry in the script and run this program. The filename does not have Np1 and Lbox, arrange this in directories is varying Np1 and Lbox
    
    sprintf(fname, "ngenic_%d.param", seed);
    //printf("%s\n", fname);
    // open file for writing; make sure to enter the appropriate directory before calling this program!
    fp = fopen(fname, "w");
    // write to the file.
    fprintf(fp, "Nmesh %d\n", Np1);
    fprintf(fp, "Nsample %d\n", Np1);
    fprintf(fp, "Box %i.0\n", Lbox);
    sprintf(filebase, "ngenIC_%d", seed);
    fprintf(fp, "FileBase %s\n", filebase);
    fprintf(fp, "OutputDir %s\n", argv[4]);
    fprintf(fp, "GlassFile /gpfs/home/sza5154/group/ICGen/N-GenIC/dummy_glass.dat\n");
    fprintf(fp, "TileFac %d\n", Np1/16);
    fprintf(fp, "Omega 0.27\nOmegaLambda 0.73\nOmegaBaryon 0.04\nHubbleParam 0.7\n");
    fprintf(fp, "Redshift %d\n", redshift);
    fprintf(fp, "Sigma8 0.8\n");
    
    fprintf(fp, "SphereMode 1\nWhichSpectrum 1\n");
    fprintf(fp, "FileWithInputSpectrum input_spectrum.txt\n");
    fprintf(fp, "InputSpectrum_UnitLength_in_cm  3.085678e24\n");
    fprintf(fp, "ReNormalizeInputSpectrum 1\n");
    fprintf(fp, "ShapeGamma 0.21\nPrimordialIndex 0.96\n");
    fprintf(fp, "Seed %d\n", seed);
    fprintf(fp, "NumFilesWrittenInParallel %d\n", nproc);
    fprintf(fp, "UnitLength_in_cm 3.085678e24\nUnitMass_in_g 1.989e43\nUnitVelocity_in_cm_per_s 1e5\n");
    close(fp);   
  }
  return 0;
}
